#pragma once
//========================================================================
// Geometry.h : Collection of code for 3D math and 3D shapes
//========================================================================

#include "Math.h"

////////////////////////////////////////////////////
//
// D3D9 Vertex Type Definitions
//
////////////////////////////////////////////////////

// A structure for our custom vertex type. We added texture coordinates
struct D3D9Vertex_Colored
{
    D3DXVECTOR3 position; // The position
    D3DCOLOR    color;    // The color

	static const DWORD FVF;
};

// A structure for our custom vertex type. We added texture coordinates
struct D3D9Vertex_ColoredTextured
{
    D3DXVECTOR3 position; // The position
    D3DCOLOR    color;    // The color
    FLOAT       tu, tv;   // The texture coordinates

	static const DWORD FVF;
};

struct D3D9Vertex_UnlitColored
{
    D3DXVECTOR3 position;	// The position in 3D space
    D3DXVECTOR3 normal;		// The normal vector (must be 1.0 units in length)
    D3DCOLOR    diffuse;    // The diffuse color
    D3DCOLOR    specular;   // The specular color

	static const DWORD FVF;
};

struct D3D9Vertex_UnlitTextured
{
    D3DXVECTOR3 position;
    D3DXVECTOR3 normal;
    FLOAT tu, tv;

    static const DWORD FVF;
};

////////////////////////////////////////////////////
//
// D3D11 Vertex Type Definitions
//
////////////////////////////////////////////////////

struct D3D11_SimpleVertex
{
    Vector3 Pos;
    Vector3 Normal;
};

struct D3D11Vertex_UnlitTextured
{
	Vector3 Pos;
	Vector3 Normal;
	Vector2 Uv;
};

// Create our vertex input layout
const D3D11_INPUT_ELEMENT_DESC D3D11VertexLayout_UnlitTextured[] =
{
    { "POSITION",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "NORMAL",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT,    0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

	
struct D3D11Vertex_UnlitTexturedColored
{
	Vector3 Pos;
	Vector3 Normal;
	Vector3 Diffuse;
	Vector2 Uv;
};

	// Create our vertex input layout
const D3D11_INPUT_ELEMENT_DESC D3D11VertexLayout_UnlitTexturedColored[] =
{
    { "POSITION",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "NORMAL",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	{ "COLOR",     0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT,    0, 36, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};


struct D3D11Vertex_PositionColored
{
	Vector3 Pos;
	Vector3 Diffuse;
};

const D3D11_INPUT_ELEMENT_DESC D3D11VertexLayout_PositionColored[] =
{
    { "POSITION",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
	{ "COLOR",     0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};


const D3D11_INPUT_ELEMENT_DESC D3D11VertexLayout_Position[] =
{
    { "POSITION",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
};


////////////////////////////////////////////////////
//
// TriangleIterator Definition - added post press
//
//    Allows a variety of different vertex buffers to be iterated as
//    a series of triangles.
//
////////////////////////////////////////////////////

class TriangleIterator
{
protected:
	Vector3 *m_Triangles;
	unsigned int m_Size;

public:
	TriangleIterator() { m_Triangles=0; m_Size=0; }
	virtual ~TriangleIterator() { SAFE_DELETE_ARRAY(m_Triangles); }

	bool InitializeStrippedMesh(LPDIRECT3DVERTEXBUFFER9 pVerts, int stride, int strips, int *triCountList );

	virtual unsigned int VGetSize() { return m_Size; }
	virtual void *VGet(unsigned int i);
};

// converts Barycentric coordinates to world coordinates
// inputs are the 3 verts of the triangle, and the u,v barycentric coordinates
extern Vector3 BarycentricToVector3(Vector3 v0, Vector3 v1, Vector3 v2, float u, float v);

extern bool IntersectTriangle( const Vector3& orig, const Vector3& dir,
                        Vector3& v0, Vector3& v1, Vector3& v2,
                        FLOAT* t, FLOAT* u, FLOAT* v );